package com.github.dote.mio.protocal;

import com.github.dote.mio.util.JsonUtil;

public class Packet {
    private int type = 0;
    private String subType = "";
    private String sessionId = "0";
    private String dataContent = "";
    private boolean ack=false;
    private String ackId = "";
    private transient int retryCount = 0;

    public Packet(){

    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getSubType() {
        return subType;
    }

    public void setSubType(String subType) {
        this.subType = subType;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getDataContent() {
        return dataContent;
    }

    public void setDataContent(String dataContent) {
        this.dataContent = dataContent;
    }

    public boolean isAck() {
        return ack;
    }

    public void setAck(boolean ack) {
        this.ack = ack;
    }

    public String getAckId() {
        return ackId;
    }

    public void setAckId(String ackId) {
        this.ackId = ackId;
    }

    public int getRetryCount() {
        return retryCount;
    }

    public void increaseRetryCount() {
        this.retryCount += 1;
    }

    public Packet clone() {
        Packet packet=new Packet();
        packet.setType(this.type);
        packet.setDataContent(this.dataContent);
        packet.setSessionId(this.sessionId);
        packet.setAck(this.ack);
        packet.setAckId(this.ackId);
        packet.setSubType(this.subType);
        return packet;
    }

    public byte[] toBytes() {
        try{
            byte[] buff = JsonUtil.toJsonString(this).getBytes(CharsetHelper.ENCODE_CHARSET);
            return buff;
        }catch (Exception e){
            return  null;
        }

    }
}
